import 'dart:ui';

import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

///tab导航item
class BaseTabNavItem extends StatefulWidget {
  ///导航类型
  final NavType navType;

  ///item 文本（仅文字和图文导航时传）
  final String text;

  ///item 图片地址（仅图片和图文导航时传）
  final String imgUrl;

  ///图片宽度
  final double imgWidth;

  ///图片高度
  final double imgHeight;

  ///item激活背景色
  final Color activeColor;

  ///item默认背景色
  final Color defaultColor;

  ///背景圆角
  final double radius;

  final int index;
  final int curIndex;

  BaseTabNavItem copyWith(int index, int curIndex) {
    return BaseTabNavItem(
      activeColor: activeColor,
      navType: navType,
      text: text,
      imgUrl: imgUrl,
      imgWidth: imgWidth,
      imgHeight: imgHeight,
      defaultColor: defaultColor,
      radius: radius,
      index: index,
      curIndex: curIndex,
    );
  }

  BaseTabNavItem(
      {super.key,
      this.activeColor = Colors.transparent,
      this.defaultColor = Colors.transparent,
      this.navType = NavType.text,
      this.text = '',
      this.imgUrl = '',
      this.imgWidth = 30,
      this.imgHeight = 30,
      this.radius = 0,
      this.index = 0,
      this.curIndex = 0});

  // @override
  // String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
  //   super.toString();
  //   return 'Tab';
  // }

  @override
  State<StatefulWidget> createState() {
    return _BaseTabNavItem();
  }
}

class _BaseTabNavItem extends State<BaseTabNavItem> {
  
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: widget.index == widget.curIndex ? widget.activeColor : widget.defaultColor,
          borderRadius: BorderRadius.all(Radius.circular(widget.radius))),
      child: _getWidget(),
    );
  }

  ///获取widget
  Widget _getWidget() {
    switch (widget.navType) {
      case NavType.text:
        return Tab(
          text: widget.text,
        );
      case NavType.imgText:
        return Tab(
          text: widget.text,
          icon: BaseImage(
            url: widget.imgUrl,
            width: widget.imgWidth,
            height: widget.imgHeight,
          ),
        );
      case NavType.img:
        return Tab(
          icon: Padding(padding: EdgeInsets.all(7),
          child: BaseImage(
            url: widget.imgUrl,
            width: widget.imgWidth,
            height: widget.imgHeight,
          ),)
        );
    }
  }
}

///导航类型
enum NavType {
  ///文本导航
  text,

  ///图文导航
  imgText,

  ///图片导航
  img
}
